using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 池子数据
/// </summary>
public class PoolData
{
    public GameObject fatherObj;
    List<GameObject> poolList;

    public int Count
    {
        get { return poolList.Count; }
    }

    public PoolData(GameObject obj, GameObject poolObj)
    {
        fatherObj = new GameObject(obj.name);
        fatherObj.transform.parent = poolObj.transform;
        poolList = new List<GameObject>() { obj };
        Push(obj);
    }

    /// <summary>
    /// 弹出
    /// </summary>
    /// <param name="resUrl"></param>
    /// <returns></returns>
    public void Pop(string resUrl, UnityAction<GameObject> callback)
    {
        if (poolList.Count > 0)
        {
            GameObject obj = poolList[0];
            obj.SetActive(true);
            obj.transform.parent = null;
            poolList.RemoveAt(0);
            callback(obj);
            return;
        }
        //异步加载
        ResMgr.GetInstance().LoadAsync<GameObject>(resUrl, (newObj) =>
        {
            newObj.name = resUrl;
            callback(newObj);
        });
        // obj = GameObject.Instantiate(Resources.Load<GameObject>(resUrl));
        // //对象名与缓存池名称一样
        // obj.name = resUrl;
    }

    /// <summary>
    /// 压入
    /// </summary>
    /// <param name="obj"></param>
    public void Push(GameObject obj)
    {
        obj.SetActive(false);
        obj.transform.parent = fatherObj.transform;
        poolList.Add(obj);
    }
    
    /// <summary>
    /// 清理
    /// </summary>
    public void Clear()
    {
        poolList.Clear();
    }
}

/// <summary>
/// 缓存池模块
/// </summary>
public class PoolMgr : Singleton<PoolMgr>
{
    public Dictionary<string, PoolData> poolDic = new Dictionary<string, PoolData>();
    private GameObject poolObj;

    /// <summary>
    /// 弹出
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public void Pop(string resUrl, UnityAction<GameObject> callback)
    {
        if (poolDic.ContainsKey(resUrl) && poolDic[resUrl].Count > 0)
        {
            poolDic[resUrl].Pop(resUrl, callback);
            return;
        }

        ResMgr.GetInstance().LoadAsync<GameObject>(resUrl, (obj)=>
        {
            //对象名与缓存池名称一样
            obj.name = resUrl;
            callback(obj);
        });
    }

    /// <summary>
    /// 压入
    /// </summary>
    /// <param name="name"></param>
    /// <param name="obj"></param>
    public void Push(string resUrl, GameObject obj)
    {
        if (poolObj == null)
            poolObj = new GameObject("Pool");

        //失活隐藏
        obj.SetActive(false);
        if (poolDic.ContainsKey(resUrl))
        {
            poolDic[resUrl].Push(obj);
        }
        else
        {
            poolDic.Add(resUrl, new PoolData(obj, poolObj));
        }
    }

    /// <summary>
    /// 清空
    /// 切换场景必须调用
    /// </summary>
    public void Clear()
    {
        foreach (PoolData pool in poolDic.Values)
            pool.Clear();
        poolDic.Clear();
        poolObj = null;
    }
}
